{% extends "xadmin/base_site.html" %}
{% load static %}
{# 导入 样式#}
{% block extrastyle %}
    <link rel="stylesheet" href="{% static 'style.css' %}">
{% endblock %}
{% block content-nav %}
{% endblock %}

{% block content %}
    <nav class="navbar navbar-default navbar-static-top">
      <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav nav-tabs nav-justified">
            <li><a href="{% url 'xadmin:statisticsQuestion' %}?id={{ meeting_obj.id }}">存在问题 <span class="sr-only">(current)</span></a></li>
            <li class="active"><a href="{% url 'xadmin:statisticsResult' %}?id={{ meeting_obj.id }}">结果统计</a></li>
          </ul>
            <input type="text" class="meeting_id" hidden value="{{ meeting_obj.id }}">
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    <div class="top" style="text-align: center">
    <span style="font-size: 20px;">评分结果</span>
{#        <a href="{% url 'xadminData:exportStatistics' %}?meeting_id={{ meeting_obj.id }}" style="float: right; margin-right: 20px; margin-bottom: 10px;"><button class="btn btn-primary btn_setting submit">导出结果</button></a>#}
        <button class="btn btn-primary btn_setting submit" style="margin-bottom: 10px;float: right; margin-right: 20px;">导出结果</button>
{#        <button class="btn btn-primary btn_setting" data-toggle="modal" data-target="#myModal" style="margin-bottom: 10px;float: right; margin-right: 20px;"><a href="{% url 'xadminData:exportStatistics' %}?meeting_id={{ meeting_obj.id }}">导出结果</a></button>#}
        <button class="btn btn-primary btn_setting statistics" style="margin-bottom: 10px;float: right; margin-right: 20px;">结果统计</button>
    </div>
    <input type="text" class="status" hidden value="{{ meeting_obj.gradeStatus }}">
    <div class="content" style="    margin-top: 100px;">
        <table class="table table-hover table-bordered">
        <thead>
            <tr class="title">
              <th>学生姓名</th>
              <th>学生学号</th>
              <th>导师</th>
              <th>申请表成绩</th>
              <th>学生互评成绩</th>
              <th>导师评分成绩</th>
              <th>最终分数</th>
              <th>奖助</th>
              <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for avg_applicationForm in avg_applicationForm_list %}
            <tr class="data-list">
                <td>{{ avg_applicationForm.0.applicationForm.sname }}</td>
                <td>{{ avg_applicationForm.0.applicationForm.sno }}</td>
                <td>{{ avg_applicationForm.0.applicationForm.student.tutor }}</td>
                <td>{{ avg_applicationForm.0.grade1 }}</td>
                <td>{{ avg_applicationForm.0.grade2 }}</td>
                <td>{{ avg_applicationForm.0.grade3 }}</td>
                <td>{{ avg_applicationForm.1 }}</td>
                <td style="width: 500px;">
                    {% for grantLevel in grantLevel_list %}
                        <div class="checkbox">
                            <label>
                              <input onclick="checkboxOnclick(this)" data="{{ grantLevel.sort }}" name="{{ avg_applicationForm.applicationForm.id }}" class="{{ avg_applicationForm.applicationForm.sname }}" type="checkbox"{% if grantLevel in avg_applicationForm.2 %} checked {% endif %} value="{{ avg_applicationForm.applicationForm.id }}, {{ grantLevel.id }}">{{ grantLevel }}
                            </label>
                        </div>
                    {% empty %}

                    {% endfor %}
                </td>
                <td style="width: 200px;">
{#                    <button class="btn btn-primary btn_setting btn-sm save-one" value="{{ avg_applicationForm.applicationForm.id }}">提交</button>#}
                    <button class="btn btn-primary btn-sm save-one" value="{{ avg_applicationForm.applicationForm.id }}"> 提交</button>
                </td>
            </tr>
            </tr>
            {% empty %}
                <tr>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                    <td>暂无内容</td>
                </tr>
            {% endfor %}
        </tbody>
        </table>
    </div>
    <div class="test"></div>
<script>
// 修改 [申请id, 等级id]
    function checkboxOnclick(checkbox){

        if ( checkbox.checked == true){
            // 获取name
           var sort = $(checkbox).attr("data");
           var sort_checked_list = []
           var a = checkbox.name
           // 获取所有name为a的选中的
           $("input[name="+a+"]:checked").each(function (i) {
               if(i < $("input[name="+a+"]:checked").length-2) {
                   sort_checked_list.push($.trim($(this).attr("data")))
               }

           })
           var b= sort_checked_list.indexOf($.trim(sort))
           if(b != -1){
               alert("同一种类型只能选一个!")
               checkbox.checked = false
           }
        }else{

        //Action for not checked

        }

        }

    // 提交
    $(".save-one").click(function () {
        var app_id = $(this).val()
        var valueList = $("input:checkbox[name="+app_id+"]:checked");
        if(valueList.length < 1){
            alert("请至少选中一项")
        }else {
            var dataList = []
           $("input:checkbox[name="+app_id+"]:checked").each(function (i) {
              dataList.push($(this).val())
            })
            $.ajax({
                url: "{% url 'xadmin:statisticsResult' %}",
                type: 'post',
                data: {"dataList": JSON.stringify(dataList), csrfmiddlewaretoken: '{{ csrf_token }}',
                cache: false,
                success: function (data) {
                    alert("成功")
                }
                }
            })
        }
    })
    $(document).ready(function () {
        var status = $(".status").val()
        if($.trim(status) == "已结束"){
            $("input:checkbox").attr("disabled", "disabled");
            $(".save-one").attr("disabled", "disabled");
        }
    })
    $(".submit").click(function () {
        var result = confirm("你确定会议没有问题了,并且已经进行了结果统计,确定导出?")
        if(result == true){
            var meeting_id = $(".meeting_id").val();
            window.location.href = "{% url 'xadminData:exportStatistics' %}?meeting_id="+meeting_id
        }
    })
    $(".statistics").click(function () {
        var meeting_id = $(".meeting_id").val();
        $.post("{% url 'dataManagement:StatisticsGrade' %}", {"id": meeting_id, csrfmiddlewaretoken: '{{ csrf_token }}'}, function (data){
            window.location.reload()
        })
    })
</script>
{% endblock %}